<?php
/**
 *
 * Author: TheSmile
 * Date: 2023/5/15 15:47
 * Email: <594652523@qq.com>
 */

namespace App\Util;

use Exception;
use Illuminate\Support\Facades\Log;

class YsyApiRequest
{
    public $requestName = '萤石云';
    public $readTimeout = 60;
    public $connectTimeout = 20;
    public $method = 'get';
    public $requestUrl;


    public function setRequestUrl($url)
    {
        $this->requestUrl = $url;
    }

    public function __construct()
    {
    }

    /**
     * @param mixed $requestName
     */
    public function setRequestName($requestName): void
    {
        $this->requestName = $requestName;
    }

    public function setMethod($method)
    {
        $this->method = $method;
    }

    public function sendRequest($paramArr = [], $headerParam = [])
    {
        $paramArr['appKey']    = config('app.ysy_app_key');
        $paramArr['appSecret'] = config('app.ysy_app_secret');
        Log::info($this->requestName . '请求参数', $paramArr);
        Log::info($this->requestName . 'header参数', $headerParam);
        if ($this->method == 'get') {
            //组织参数
            $strParam         = http_build_query($paramArr);
            $this->requestUrl .= '?' . $strParam;
            return $this->curl($this->requestUrl, true, [], $headerParam);
        } else {
            return $this->curl($this->requestUrl, true, $paramArr, $headerParam);
        }

    }

    public function curl($url, $json = true, $postFields = null, $headerParam = [])
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $header = array("content-type: application/x-www-form-urlencoded; charset=UTF-8");
        if ($headerParam) {
            $header = array_merge($header, $headerParam);
        }
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        if ($this->readTimeout) {
            curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
        }
        if ($this->connectTimeout) {
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
        }
        curl_setopt($ch, CURLOPT_USERAGENT, "top-sdk-php");
        //https 请求
        if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https") {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }

        if (is_array($postFields) && 0 < count($postFields)) {
            $postBodyString = "";
            foreach ($postFields as $k => $v) {
                $postBodyString .= "$k=" . urlencode($v) . "&";
            }
            unset($k, $v);


            curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1));
            curl_setopt($ch, CURLOPT_POST, true);

        }


        $reponse = curl_exec($ch);

        if (curl_errno($ch)) {
            throw new Exception(curl_error($ch), 0);
        } else {
            $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (200 !== $httpStatusCode) {
                throw new Exception($reponse, $httpStatusCode);
            }
        }
        curl_close($ch);
        if ($json)
            return json_decode($reponse, true);
        return $reponse;
    }

}
